<?php

class Excel {

    private $excel;

    public function __construct() {
        // initialise the reference to the codeigniter instance
        require_once APPPATH.'third_party/PHPExcel.php';
        //require_once APPPATH.'third_party/PHPExcel/IOFactory.php';
        $this->excel = new PHPExcel();  
        $this->cloneExcel= new PHPExcel(); 
    }
    
    public function load($path) {
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
        $this->excel = $objReader->load($path);
    }
    
    public function cloneSheet(){
        
        //  Set any styles here against the currently active sheet in $objPHPExcel

        //  Get the current sheet with all its newly-set style properties
        $objWorkSheetBase = $this->excel->getSheet(0);
        
        //  Create a clone of the current sheet, with all its style properties
        $this->cloneExcel = clone $objWorkSheetBase;
        //  Set the newly-cloned sheet title
        $this->cloneExcel->setTitle('Sheet1');
        //  Attach the newly-cloned sheet to the $objPHPExcel workbook
        $this->excel->addSheet($this->cloneExcel);
    }
    
    public function save($path) {
        // Write out as the new file
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        $objWriter->save($path);
    }

    public function stream($filename) {       
        header('Content-type: application/ms-excel');
        header("Content-Disposition: attachment; filename=\"".$filename."\""); 
        header("Cache-control: private");        
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        $objWriter->save('php://output');    
    }

    public function  __call($name, $arguments) {  
        // make sure our child object has this method  
        if(method_exists($this->excel, $name)) {  
            // forward the call to our child object  
            return call_user_func_array(array($this->excel, $name), $arguments);  
        }  
        return null;  
    }  
}

?>